package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;

import object.Company;
import utility.StringUtil;
import clientMain.Login;

public class CompanyDelegate {

	
	public Company getCompany(int comId){
		Company company = new Company();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{ call sp_getCompany(?) }");
			cst.setInt(1, comId);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){	
				company.setCompanyId(comId);
				company.setCompanyName(StringUtil.getFilterIllegal(rs.getString("Company_Name")));
				company.setCompanyType(StringUtil.getFilterIllegal(rs.getString("Company_Type")));
				company.setCompanyIndustry(StringUtil.getFilterIllegal(rs.getString("Company_Industry")));
				company.setCompanyFounder(StringUtil.getFilterIllegal(rs.getString("Company_Founder")));
				company.setCompanyFounded(rs.getString("DateFounded"));
				company.setAddress(StringUtil.getFilterIllegal(rs.getString("Company_Headquarters")), 
						StringUtil.getFilterIllegal(rs.getString("DistrictName")), 
						StringUtil.getFilterIllegal(rs.getString("ProvinceName")));
				company.setCompanyPhone(StringUtil.getFilterIllegal(rs.getString("Company_Phone")));
				company.setCompanyWebsite(StringUtil.getFilterIllegal(rs.getString("Company_Website")));
				company.setCompanyTotalDep(rs.getInt("Company_TotalDep"));
				company.setCompanyTotalEm(rs.getInt("Company_TotalEm"));
			}
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			company = null;
		}
		return company;
	}

		public boolean updateCompany(Company Con,int userUpdate){
			boolean flag = false;
			try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateCompany(?,?,?,?,?,?,?,?,?,?,?)}");
			cst.setInt(1, Con.getCompanyId());
			cst.setString(2, Con.getCompanyName());
			cst.setString(3, Con.getCompanyType());
			cst.setString(4, Con.getCompanyIndustry());
			cst.setString(5, Con.getCompanyFounded());
			cst.setString(6, Con.getCompanyFounder());
			cst.setString(7, Con.getCompanyHeadquarters());
			cst.setInt(8,Con.getCompanyCity());
			cst.setString(9, Con.getCompanyPhone());
			cst.setString(10, Con.getCompanyWebsite());
			cst.setInt(11, userUpdate );
			cst.executeUpdate();
			Login.conn.commit();
			flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return flag;
		}
		
		public static Company getCompany1(int comId){
			Company company = new Company();
			try {
				Login.conn.setAutoCommit(false);
				CallableStatement cst = Login.conn.prepareCall("{ call sp_getCompany(?) }");
				cst.setInt(1, comId);
				ResultSet rs = cst.executeQuery();
				while(rs.next()){	
					company.setCompanyId(comId);
					company.setCompanyName(StringUtil.getFilterIllegal(rs.getString("Company_Name")));
					company.setCompanyType(StringUtil.getFilterIllegal(rs.getString("Company_Type")));
					company.setCompanyIndustry(StringUtil.getFilterIllegal(rs.getString("Company_Industry")));
					company.setCompanyFounder(StringUtil.getFilterIllegal(rs.getString("Company_Founder")));
					company.setCompanyFounded(rs.getString("DateFounded"));
					company.setAddress(StringUtil.getFilterIllegal(rs.getString("Company_Headquarters")), 
							StringUtil.getFilterIllegal(rs.getString("DistrictName")), 
							StringUtil.getFilterIllegal(rs.getString("ProvinceName")));
					company.setCompanyPhone(StringUtil.getFilterIllegal(rs.getString("Company_Phone")));
					company.setCompanyWebsite(StringUtil.getFilterIllegal(rs.getString("Company_Website")));
					company.setCompanyTotalDep(rs.getInt("Company_TotalDep"));
					company.setCompanyTotalEm(rs.getInt("Company_TotalEm"));
				}
				Login.conn.commit();
			} catch (Exception e) {
				e.printStackTrace();
				company = null;
			}
			return company;
		}
}

